package com.ruoyi.netty.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * InfluxDB配置
 */
//@Configuration
@ConfigurationProperties(prefix = "influxdb")
@Data
@Slf4j
public class InfluxDBConfig {
    // 静态常量定义
    private static final String DEFAULT_RETENTION_POLICY = "autogen";

    /**
     * 连接地址
     */
    private String url;

    /**
     * 认证token
     */
    private String token;

    /**
     * 组织
     */
//    private String org;

    /**
     * 存储桶
     */
    private String bucket;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 数据库名称
     */
    private String database;

    /**
     * 保留策略
     */
    private String retentionPolicy = DEFAULT_RETENTION_POLICY;

    /**
     * 连接超时时间(秒)
     */
    private int connectTimeout = 10;

    /**
     * 写入超时时间(秒)
     */
    private int writeTimeout = 10;

    /**
     * 读取超时时间(秒)
     */
    private int readTimeout = 10;

    @Bean
    public InfluxDB influxDBClient() {
        log.info("Initializing InfluxDB client with URL: {}", url);
        InfluxDB influxDB = InfluxDBFactory.connect(url, username, password);

        // 设置数据库
        influxDB.setDatabase(database);

        // 测试连接
        try {
            String version = influxDB.version();
            log.info("Connected to InfluxDB version: {}", version);
        } catch (Exception e) {
            log.error("Failed to connect to InfluxDB: {}", e.getMessage());
        }

        return influxDB;
    }
}
